<template>
  <div class="leadershippfcy">
    <div class="top">
      <div class="imgdiv">
        <!--        <img src="../../assets/khImg/back.png" @click="tuichuBtn" alt="返回">-->
      </div>
      <div class="title">{{programmeName}}</div>
      <div class="divBtn">
        <el-button  @click="tuichuBtn">返回</el-button>
      </div>
    </div>
    <div class="bottom">
      <div class="left">
        <div class="title khmname">
          考核码
        </div>
        <div class="kaoheSum">
          {{khm}}
        </div>
        <div class="bottom">
          <div class="youxiu">允许优秀部门: <span style="color: #f24444">{{maxExcellent}}</span> </div>
          <div class="youxiu">已评价优秀部门数：<span style="color: #f24444">{{hasExcellent}}</span></div>
          <div class="youxiu">剩余优秀部门数：<span style="color: #f24444">{{remainExcellent}}</span></div>
        </div>
      </div>
      <div class="right">
        <div class="title">
          <div class="titlediv"> {{explainText}}</div>
        </div>
        <div class="tablestyle" >
          <table border="0"  frame=void >
            <tr class="hangname">
              <td  v-for="item in listLabel">
                <span style="cursor:pointer;" v-html="item.name"></span>
              </td>
            </tr>
            <tr  class="liename" v-for="(item,liexb) in tableData">
              <td  v-for="(itemx,dianxb) in item">
                <span  style="color: #000000;cursor:pointer;" v-show="!itemx.deptId && itemx.value!='' && !itemx.totalScore"  @click="biaoBtn(itemx,item,dianxb,liexb)"> {{itemx.value}}</span>
                <span class="dafen"  @mouseenter="changeActive($event)" @mouseleave="removeActive($event)" v-show="itemx.value=='' && !itemx.totalScore" @click="biaoBtn(itemx,item,dianxb,liexb)">
                  打分
                </span>
                <span v-show="itemx.deptId" >
                  <span style="cursor:pointer;color: #1c84c6" v-show="itemx.reportPath" @click="bumenBtn(itemx)">{{itemx.value}}</span>
                  <span v-show="!itemx.reportPath" >{{itemx.value}}</span>
                </span>
                <span v-show="itemx.totalScore">
                  <span v-show="itemx.value>'0' && itemx.value<'100'"> {{itemx.value}} ({{itemx.xiuer}})</span>
                  <span style="color: red;" v-show="itemx.value=='0' || itemx.value=='100'"> {{itemx.value}}</span>
                </span>
              </td>
            </tr>
          </table>
          <div class="buttonDiv">
            <el-button class="baocundiv" type="success"  @click="saveBtn">保存</el-button>
            <el-button class="tijiaodiv" type="success"   @click="tijiao">提交</el-button>
          </div>
        </div>
      </div>

    </div>


    <dafen-Dialog v-if="kucunShow" :jihuaData="kuncunData" @callback="kucunBack"></dafen-Dialog>
  </div>
</template>


<script>
  import request from '@/utils/request'
  import dafenDialog from "./dafenDialog";
  import Cookies from 'js-cookie'
  export default {
    name: "leadership",
    components:{
      dafenDialog
    },
    data() {
      return {
        value:'',
        options:[
          {
            value: '选项1',
            label: '黄金糕'
          }, {
            value: '选项2',
            label: '双皮奶'
          }
        ],
        remainExcellent:'',
        maxExcellent:'',
        hasExcellent:'',
        queryParams:{
          deptId:'',
          zhanshi:'',
        },
        kuncunData:'',
        kucunShow:false,
        loading:false,
        explainText:'',
        programmeName:'',
        //表头数据
        listLabel: [
          {indexName: '公司名称', prop: 'carFundProvider'},
          {indexName: '利润', prop: 'carFrameNo'},
          {indexName: '大类', prop: 'carNo'},
          {indexName: '小类', prop: 'acreage'},
        ],
        tableData: [],
        assessmentId:'',
        pattern:'',
        programmeId:'',
        khm:'',
        baseURL:process.env.VUE_APP_BASE_API,
        jilu:1,
        scoreLevels:[],
        locked:true,
        excellentId:[]
      };
    },

    created() {
      this.khm = Cookies.get('khm')
      this.getList()

    },
    methods: {
      changeActive($event) {
        $event.currentTarget.className = 'dafenhr';
      },
      removeActive($event) {
        $event.currentTarget.className = 'dafen';
      },
      //部门点击事件
      bumenBtn:function(row){
        console.log('部门',row)
        // this.download(response.msg);
        // window.location.href = this.baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
        window.location.href = this.baseURL + "/common/download/resource?name=" + encodeURI(row.reportPath) + "&delete=" + true;
      },
      //判断是否所有数据都打分
      allDafen:function(){
        debugger
        for (var i=0;i<this.tableData.length;i++){
          for (var p=0;p<this.tableData[i].length;p++){
            if(!this.tableData[i][p].deptId){
              if(this.tableData[i][p].value==''){
                this.$message({
                  type: 'error',
                  message: '还有未打分的，不能提交'
                });
                return true
              }
            }
          }
        }
      },
      //提交
      tijiao:function(){
        var allDafen = this.allDafen()
        if(allDafen){
          return
        }
        this.loading = true
        var obj = {}
        obj.assessItems = this.tableData
        obj.assessRules = this.listLabel
        obj.assessmentId = this.assessmentId
        obj.explainText = this.explainText
        obj.pattern = this.pattern
        obj.programmeId = this.programmeId
        obj.programmeName = this.programmeName
        obj.locked = true
        request({
          url: '/kh/fe/programme',
          method: 'post',
          data:obj
        }).then(response => {
          this.$message({
            type: 'success',
            message: '提交成功'
          });
          this.loading = false;
          this.getList()

        });
      },
      //保存
      saveBtn:function(){
        this.loading = true
        var obj = {}
        obj.assessItems = this.tableData
        obj.assessRules = this.listLabel
        obj.assessmentId = this.assessmentId
        obj.explainText = this.explainText
        obj.pattern = this.pattern
        obj.programmeId = this.programmeId
        obj.programmeName = this.programmeName
        obj.locked = this.locked
        request({
          url: '/kh/fe/programme',
          method: 'post',
          data:obj
        }).then(response => {
          this.$message({
            type: 'success',
            message: '保存成功'
          });
          this.loading = false;
          this.getList()

        });

      },
      tuichuBtn:function(){
        this.$router.push('/homePage');
      },
      //获取列表数据
      getList:function() {
        this.loading = true
        request({
          url: '/kh/fe/programme/'+this.$route.query.data,
          method: 'get',
        }).then(response => {
          this.programmeName = response.data.programmeName
          this.explainText = response.data.explainText
          this.listLabel = response.data.assessRules
          this.tableData = response.data.assessItems
          this.assessmentId = response.data.assessmentId
          this.pattern = response.data.pattern
          this.programmeId = response.data.programmeId
          this.loading = false;
          this.locked = response.data.locked
          this.scoreLevels = response.data.scoreLevels
          this.maxExcellent = response.data.excellentNum
          this.remainExcellent = this.maxExcellent
          this.hasExcellent = 0
          this.excellentId = []

          for (var i=0;i<this.tableData.length;i++){
            this.hejiSum(this.tableData[i],this.tableData[i].length)

          }
        });
      },

      //打分按钮点击
      biaoBtn:function(rowx,row,dianxb,liexb) {

        var obj={}
        obj.gsname=  row[0].assessItemName
        obj.lxname = rowx.headName
        obj.level = rowx.level
        obj.value = rowx.value
        obj.dianxb = dianxb
        obj.liexb = liexb
        this.kuncunData = obj
        this.kucunShow = true
      },
      //回调函数
      kucunBack:function(msg) {
        if(!msg){
          this.kucunShow = false
          return
        }
        this.jilu = this.tableData[msg.liexb][msg.dianxb].value
        this.kucunShow = false
        console.log('返回数据',msg)
        if(msg==''){

        }else {
          this.tableData[msg.liexb][msg.dianxb].value = msg.value
          //合计总分
          this.hejiSum(this.tableData[msg.liexb],this.tableData[msg.liexb].length)
          var pp = this.hejiSum(this.tableData[msg.liexb],this.tableData[msg.liexb].length)

          if(pp==100){
            this.tableData[msg.liexb][msg.dianxb].value = this.jilu
          }else {
            // this.saveBtn()
            if(pp == 0){
              this.tableData[msg.liexb][msg.dianxb].value = this.jilu
            }
          }
        }

      },
      //合计总分
      hejiSum:function(arr,leg) {
        var kongSum=0
        for (var i=0;i<arr.length-1;i++){
          if (!isNaN(Number(arr[i].value))) {
            kongSum = kongSum + arr[i].value*1
          }

        }
        if(kongSum=='100'){
          this.$message({
            type: 'error',
            message: '提示：总分不能为100分，否则为废票。'
          });
          return 100
        }
        var rankLabel = ""
        for (var j=0;j<this.scoreLevels.length;j++){
          if(kongSum/1>=this.scoreLevels[j].min/1 && kongSum/1<=this.scoreLevels[j].max/1){
            rankLabel =this.scoreLevels[j].label
            break
          }
        }

        if("优秀"=== rankLabel){
          if(this.excellentId.find(id => id === arr[0].assessItemId)){
          } else {
            if(this.hasExcellent + 1 > this.maxExcellent){
              this.$message({
                type: 'error',
                message: '提示：超过最大优秀人数'
              });
              return 0
            }
            this.excellentId.push(arr[0].assessItemId)
          }
        } else {
          if(this.excellentId.find(id => id === arr[0].assessItemId)){
            this.excellentId.splice(this.excellentId.indexOf(arr[0].assessItemId), 1)
          }
        }

        arr[leg-1].value =kongSum
        arr[leg-1].xiuer =rankLabel


        this.hasExcellent = this.excellentId.length
        this.remainExcellent = this.maxExcellent - this.hasExcellent
        return 1

      },

    }
  };
</script>

<style  lang="scss" scoped>
  .leadershippfcy {
    margin: 0;
    padding: 0;
    .top {
      position: fixed;
      top: 0;
      left: 0;
      display: flex;
      z-index: 9;
      align-items: center;
      justify-content: space-between;
      width: 100%;
      height: 100px;
      background-color: #1085ca;
      .imgdiv {
        margin-left: 60px;
        img {
          width: 48px;
          height: 48px;
        }
      }
      .title {
        height: 30px;
        font-family: MicrosoftYaHei;
        font-size: 30px;
        font-weight: normal;
        font-stretch: normal;
        line-height: 20px;
        letter-spacing: 0px;
        color: #ffffff;
      }
      .divBtn {
        margin-right: 60px;
        .el-button {
          width: 121px;
          height: 40px;
          background-color: #f6f6f6;
          border-radius: 6px;
          font-family: MicrosoftYaHei;
          font-size: 18px;
          font-weight: normal;
          font-stretch: normal;
          line-height: 20px;
          letter-spacing: 0px;
          color: #0186d1;
        }
      }
    }
    .bottom {
      width: 100%;
      .left {
        padding-top: 100px;
        position: absolute;
        width: 276px;
        height: 100%;
        background-color: #ffffff;
        box-shadow: 0px 11px 21px 0px
        rgba(0, 10, 15, 0.33);
        .khmname {
          margin-top: 59px;
        }
        .title {
          margin-left: 44px;
          font-family: MicrosoftYaHei;
          font-size: 18px;
          font-weight: normal;
          font-stretch: normal;
          line-height: 19px;
          letter-spacing: 0px;
          color: #000000;
          margin-bottom: 12px;
        }
        .kaoheSum {
          margin-left: 44px;
          margin-bottom: 24px;
          font-family: MicrosoftYaHei;
          font-size: 18px;
          font-weight: normal;
          font-stretch: normal;
          line-height: 19px;
          letter-spacing: 0px;
          color: #087ab9;
        }
        .el-select {
          margin-left: 44px;
          margin-bottom: 24px;
        }
        .bottom {
          position: absolute;
          bottom: 72px;
          left: 44px;
          .youxiu {
            font-family: MicrosoftYaHei;
            font-size: 14px;
            font-weight: normal;
            font-stretch: normal;
            letter-spacing: 0px;
            color: #000000;
            margin-top: 29px;
          }

        }
      }
      .right {
        width: 100%;
        padding-top: 100px;
        padding-left: 276px;
        .title {
          width: 100%;
          height: 61px;
          background-color: #f2f2f2;
          .titlediv {
            width: 1012px;
            padding-top: 15px;
            margin-left: 60px;
            height: 32px;
            font-family: MicrosoftYaHei;
            font-size: 14px;
            font-weight: normal;
            font-stretch: normal;
            line-height: 19px;
            letter-spacing: 0px;
            color: #f24444;
          }

        }
        .tablestyle {
          width: 1524px;
          margin: auto;
          margin-top: 41px;

          table {
            width: 100%;
            text-align: center;
            /*border-spacing:20px;*/

            .hangname {
              width: 100%;
              height: 55px;
              background-color: #0186d1;
              border-radius: 6px;
              font-family: MicrosoftYaHei;
              font-size: 18px;
              font-weight: normal;
              font-stretch: normal;
              line-height: 19px;
              letter-spacing: 0px;
              color: #ffffff;
            }
            .liename {

              width: 100%;
              height: 55px;
              background-color: #ecf3ff;
              border-radius: 6px;
              border: solid 1px #e5e5e5;
              font-family: MicrosoftYaHei;
              font-size: 18px;
              font-weight: normal;
              font-stretch: normal;
              letter-spacing: 0px;
              color: #2a2a2a;
              .dafen {
                display: inline-block;
                width: 63px;
                height: 29px;
                border-radius: 15px;
                border: solid 1px #0186d1;
                font-family: MicrosoftYaHei;
                font-size: 18px;
                font-weight: normal;
                font-stretch: normal;
                line-height: 29px;
                letter-spacing: 0px;
                color: #0186d1;
                cursor:pointer;
              }
              .dafenhr {
                display: inline-block;
                width: 63px;
                height: 29px;
                border-radius: 15px;
                background-color: #0186d1;
                border: solid 1px #0186d1;
                font-family: MicrosoftYaHei;
                font-size: 18px;
                font-weight: normal;
                font-stretch: normal;
                line-height: 29px;
                letter-spacing: 0px;
                color:#ffffff;
                cursor:pointer;
              }
            }
          }

        }
        .buttonDiv {
          height: 55px;
          margin-top: 40px;
          width: 100%;
          text-align: center;
          .baocundiv {
            width: 93px;
            height: 55px;
            background-color: #00c882;
            border-radius: 9px;
            font-family: MicrosoftYaHei;
            font-size: 18px;
            font-weight: normal;
            font-stretch: normal;
            line-height: 19px;
            letter-spacing: 0px;
            color: #ffffff;
          }
          .tijiaodiv {
            width: 93px;
            height: 55px;
            margin-left: 128px;
            background-color: #0186d1;
            border-radius: 9px;
            font-family: MicrosoftYaHei;
            font-size: 18px;
            font-weight: normal;
            font-stretch: normal;
            line-height: 19px;
            letter-spacing: 0px;
            color: #ffffff;
          }

        }
      }

    }
  }
  @media screen and (max-width:800px){
    .leadershippfcy {
      margin: 0;
      padding: 0;
      .top {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 9;
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        height: 5em;
        background-color: #1085ca;
        overflow: hidden;
        .imgdiv {
          margin-left: 60px;
          img {
            width: 48px;
            height: 48px;
          }
        }
        .title {
          height: 100%;
          font-family: MicrosoftYaHei;
          font-size: 1.3em;
          font-weight: normal;
          font-stretch: normal;
          line-height: 4em;
          letter-spacing: 0px;
          color: #ffffff;
        }
        .divBtn {
          margin-right: 60px;
          .el-button {
            width: 6em;
            height: 2.2em;
            background-color: #f6f6f6;
            border-radius: 6px;
            font-family: MicrosoftYaHei;
            font-size: 1em;
            font-weight: normal;
            font-stretch: normal;
            line-height: 1em;
            letter-spacing: 0px;
            color: #0186d1;
          }
        }
      }
      .bottom {
        width: 100%;
        .left {
          padding-top: 2em;
          position: absolute;
          width: 14em;
          height: 100%;
          background-color: #ffffff;
          -webkit-box-shadow: 0px 11px 21px 0px rgba(0,10,15,0.33);
          box-shadow: 0px 11px 21px 0px rgba(0,10,15,0.33);
          .khmname {
            margin-top: 4em;
          }
          .title {
            margin-left: 1em;
            font-family: MicrosoftYaHei;
            font-size: 14px;
            font-weight: normal;
            font-stretch: normal;
            line-height: 19px;
            letter-spacing: 0px;
            color: #000000;
            margin-bottom: 0.3em;
          }
          .kaoheSum {
            margin-left: 1em;
            margin-bottom: 1em;
            font-family: MicrosoftYaHei;
            font-size: 14px;
            font-weight: normal;
            font-stretch: normal;
            line-height: 19px;
            letter-spacing: 0px;
            color: #087ab9;
          }
          .el-select {
            margin-left: 1em;
            margin-bottom: 24px;
          }
          .bottom {
            position: absolute;
            bottom: 72px;
            left: 1em;
            .youxiu {
              font-family: MicrosoftYaHei;
              font-size: 14px;
              font-weight: normal;
              font-stretch: normal;
              letter-spacing: 0px;
              color: #000000;
              margin-top: 29px;
            }

          }
        }
        .right {
          width: 50em;
          padding-top: 5em;
          padding-left: 15em;
          .title {
            width: 100%;
            height: 5em;
            background-color: #f2f2f2;
            .titlediv {
              width: 100%;
              padding-top: 15px;
              margin-left: 0;
              height: 0;
              font-family: MicrosoftYaHei;
              font-size: 14px;
              font-weight: normal;
              font-stretch: normal;
              line-height: 19px;
              letter-spacing: 0px;
              color: #f24444;
            }

          }
          .tablestyle {
            width: 100%;
            margin: auto;
            margin-top: 41px;

            table {
              width: 100%;
              text-align: center;
              /*border-spacing:20px;*/

              .hangname {
                width: 100%;
                height: 55px;
                background-color: #0186d1;
                border-radius: 6px;
                font-family: MicrosoftYaHei;
                font-size: 14px;
                font-weight: normal;
                font-stretch: normal;
                line-height: 19px;
                letter-spacing: 0px;
                color: #ffffff;
              }
              .liename {

                width: 100%;
                height: 55px;
                background-color: #ecf3ff;
                border-radius: 6px;
                border: solid 1px #e5e5e5;
                font-family: MicrosoftYaHei;
                font-size: 14px;
                font-weight: normal;
                font-stretch: normal;
                letter-spacing: 0px;
                color: #2a2a2a;
                .dafen {
                  display: inline-block;
                  width: 63px;
                  height: 29px;
                  border-radius: 15px;
                  border: solid 1px #0186d1;
                  font-family: MicrosoftYaHei;
                  font-size: 14px;
                  font-weight: normal;
                  font-stretch: normal;
                  line-height: 29px;
                  letter-spacing: 0px;
                  color: #0186d1;
                  cursor:pointer;
                }
                .dafenhr {
                  display: inline-block;
                  width: 63px;
                  height: 29px;
                  border-radius: 15px;
                  background-color: #0186d1;
                  border: solid 1px #0186d1;
                  font-family: MicrosoftYaHei;
                  font-size: 14px;
                  font-weight: normal;
                  font-stretch: normal;
                  line-height: 29px;
                  letter-spacing: 0px;
                  color:#ffffff;
                  cursor:pointer;
                }
              }
            }

          }
          .buttonDiv {
            height: 55px;
            margin-top: 40px;
            width: 100%;
            text-align: center;
            .baocundiv {
              width: 5em;
              height: 2.7em;
              background-color: #00c882;
              border-radius: 9px;
              font-family: MicrosoftYaHei;
              font-size: 14px;
              font-weight: normal;
              font-stretch: normal;
              line-height: 19px;
              letter-spacing: 0px;
              color: #ffffff;
            }
            .tijiaodiv {
              width: 5em;
              height: 2.7em;
              margin-left: 3em;
              background-color: #0186d1;
              border-radius: 9px;
              font-family: MicrosoftYaHei;
              font-size: 14px;
              font-weight: normal;
              font-stretch: normal;
              line-height: 19px;
              letter-spacing: 0px;
              color: #ffffff;
            }

          }
        }

      }
    }
  }

</style>
